package com.example.strategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/6/910:42
 */
public class ShardingTableConfig {
    /**
     * 启用存储数据库位置编号
     */
    private static final List<String> tablePrefix = new ArrayList<>();

    //配置启用哪些库位前缀
    static {
        tablePrefix.add("0");
        tablePrefix.add("a");
        //a多加一个则a表权重会大一些
        //tablePrefix.add("a");
    }

    /**
     * 获取随机前缀
     * @return
     */
    public static String getRandomTablePrefix(String code){
        int hashCode = code.hashCode();
        int index = Math.abs(hashCode) % tablePrefix.size();
        return tablePrefix.get(index);
    }
}
